//
//  AllEvaluationVC.m
//  FKGW
//
//  Created by song ce on 2017/3/15.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "AllEvaluationVC.h"
#import "GoodsCellTableViewCell.h"
#import "AllEvaluateHeadV.h"
@interface AllEvaluationVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)AllEvaluateHeadV *allHeadView;
@end

@implementation AllEvaluationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self _initTopView];
    [self tableViewAndHeaderView];
    
    if(self.type.length){
         [self getEValuateGoodsWithInt:[self.type intValue]];
    }else{
        [self getEValuateGoodsWithInt:3];
    }
}

//2.5 获取商品评价接口
- (void)getEValuateGoodsWithInt:(int)count  {
    NSString *level = [NSString stringWithFormat:@"%d",count];
    NSDictionary *paraDic = @{
                              @"pid":self.pid,//商品标识
                              @"level":level,//评价等级
                              @"size":@"20",//需要返回的评论最大条数
                              @"currentpage":@"1"
                              };
    __weak typeof(self)  weakSelf = self;
      [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:EVALUATEGOODS paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"获取商品评价接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
                return ;
            };
            //评论列表
            NSArray *listArr = dataDic[@"contents"];
            if(self.dataSource.count){
                [self.dataSource removeAllObjects];
            }
            for(NSDictionary *dic in listArr){
                EvaluateListModel *model = [EvaluateListModel  modelWithDic:dic];
                [weakSelf.dataSource addObject:model];
            }
            [weakSelf.tableV reloadData];
            [MBProgressHUD dismissHUD];
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置详情页面评论显示数量
                weakSelf.allHeadView.evaluateCountLabel.text = [NSString stringWithFormat:@"商品评价  （%ld）", weakSelf.dataSource.count];
            });
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

-(void)_initTopView{
    //top视图（评价按钮）
    _allHeadView =   [[[NSBundle mainBundle]loadNibNamed:@"AllEvaluateHeadV" owner:self options:nil]lastObject];
    _allHeadView.top = kNaviBarHeight;
    _allHeadView.left = 0;
     [self.view addSubview:_allHeadView];
    
    //根据选择的评论按钮加载不同的数据
    __weak typeof(self) weakSelf = self;
    _allHeadView.evaluateBlock =^(int count){
        [weakSelf getEValuateGoodsWithInt:count];
    };
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, self.allHeadView.bottom, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight-self.allHeadView.height) style:UITableViewStylePlain];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    [self.view addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.bounces =NO;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      NSString *ident=@"actell";
    GoodsCellTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"GoodsCellTableViewCell" owner:self options:nil]lastObject];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
      return 55;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

@end
